package com.jeff.android.lab.android.lifecycle.viewmodel;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jeff.android.lab.BaseActivity;
import com.jeff.android.lab.R;
import com.jeff.android.lab.model.User;

import java.util.List;

public class ViewModelActivity extends BaseActivity {

    private ActivityViewModel mActivityViewModel;

    private UserListViewModel mUserListViewModel;

    private TextView mTvValue;

    private RecyclerView mRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutView());

        mActivityViewModel = new ViewModelProvider(this).get(ActivityViewModel.class);
        mActivityViewModel.getData().observe(this,
                new Observer<String>() {
                    @Override
                    public void onChanged(String value) {
                        mTvValue.setText(value);
                    }
                });

        mUserListViewModel = new ViewModelProvider(this).get(UserListViewModel.class);
        mUserListViewModel.getLiveData().observe(this,
                new Observer<List<User>>() {
                    @Override
                    public void onChanged(List<User> users) {
                       BaseQuickAdapter adapter = (BaseQuickAdapter)mRecyclerView.getAdapter();
                        adapter.setNewData(users);
                    }
                });
    }

    private View getLayoutView() {
        final LinearLayout root = new LinearLayout(this);
        LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        root.setLayoutParams(p);
        root.setOrientation(LinearLayout.VERTICAL);
        root.setFitsSystemWindows(true);

        Button btn = new Button(this);
        btn.setText("设置数据");
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActivityViewModel.setData("Hello World.");

                mUserListViewModel.loadData();
            }
        });
        root.addView(btn);

        mTvValue = new TextView(this);

        mTvValue.setText("你好");
        root.addView(mTvValue);

        mRecyclerView = new RecyclerView(this);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(new BaseQuickAdapter<User, BaseViewHolder>(R.layout.common_simple_item_text) {
            @Override
            protected void convert(BaseViewHolder helper, User item) {
                TextView textView = helper.getView(R.id.tv_simple_item);
                textView.setText(item.name);
            }
        });
        root.addView(mRecyclerView);

        return root;
    }

}
